/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package eyrolles.action;

import exception.*;
import eyrolles.Employe;
import eyrolles.EyrollesModele;
import eyrolles.form.EmployeForm;
import eyrolles.form.LoginForm;
import java.util.ArrayList;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts.action.*;
import org.apache.struts.actions.DispatchAction;

/**
 *
 * @author Nairo
 */
public class EmployeAction extends CotationsAction {

    public ActionForward userLoginAction(
            ActionMapping mapping,
            ActionForm form,
            HttpServletRequest request,
            HttpServletResponse response) throws ServiceException, UserLoginException {

        String user = null;

        // Cible par defaut en cas de succes
        String target = new String("success");

        // Utilisation de LoginForm pour obtenir les parametres
        // de la requete
        String username = ((LoginForm) form).getUsername();
        String password = ((LoginForm) form).getPassword();

        try {
            EyrollesModele modele = getModel();
            user = modele.connecterEmploye(username, password);
        HttpSession session = request.getSession();
        session.setAttribute("USER", user);
        } catch (UserLoginException ule) {
            target = new String("login");
            ActionMessages errors = new ActionMessages();

            errors.add(ActionMessages.GLOBAL_MESSAGE,
                    new ActionMessage("errors.login.wrongPassword", ule.getLogin()));


            // Enregistrer les erreurs  trouvees dans le formulaire original
            if (!errors.isEmpty()) {

                saveErrors(request, errors);
            }

        } catch (ServiceException se) {
            target = new String("error");
            ActionMessages errors = new ActionMessages();

            errors.add(ActionMessages.GLOBAL_MESSAGE,
                    new ActionMessage("errors.service.error", se));


            // Enregistrer les erreurs  trouvees dans le formulaire original
            if (!errors.isEmpty()) {

                saveErrors(request, errors);
            }
        } catch (DataAccessException dae) {
            target = new String("error");
            ActionMessages errors = new ActionMessages();

            errors.add(ActionMessages.GLOBAL_MESSAGE,
                    new ActionMessage("errors.dataAccess.error", dae));


            // Enregistrer les erreurs  trouvees dans le formulaire original
            if (!errors.isEmpty()) {

                saveErrors(request, errors);
            }
        }

        // Transmission de la requete a la vue appropriee
        return (mapping.findForward(target));
    }

    public ActionForward listEmployeAction(ActionMapping mapping,
            ActionForm form,
            HttpServletRequest request,
            HttpServletResponse response) throws ServiceException, UserLoginException {

        // Default target to success
        String target = new String("success");

        // Teste si l'utilisateur est identifie
        HttpSession session = request.getSession();
        if (session.getAttribute("USER") == null) {

            // The user is not logged in
            target = new String("login");
            ActionMessages errors = new ActionMessages();

            errors.add(ActionMessages.GLOBAL_MESSAGE,
                    new ActionMessage("errors.notConnected"));


            // Report any errors we have discovered back to
            // the original form
            if (!errors.isEmpty()) {
                saveErrors(request, errors);
            }

        }

        ArrayList employes = null;

        try {
            EyrollesModele modele = getModel();
            employes = modele.recupererEmployes();
        } catch (ServiceException se) {
            target = new String("error");
            ActionMessages errors = new ActionMessages();

            errors.add(ActionMessages.GLOBAL_MESSAGE,
                    new ActionMessage("errors.service.error",
                    se.getMessage()));

            // Signaler les erreurs eventuelles
            if (!errors.isEmpty()) {
                saveErrors(request, errors);
            }
        } catch (DataAccessException dae) {
            target = new String("error");
            ActionMessages errors = new ActionMessages();

            errors.add(ActionMessages.GLOBAL_MESSAGE,
                    new ActionMessage("errors.dataAccess.error", dae));


            // Enregistrer les erreurs  trouvees dans le formulaire original
            if (!errors.isEmpty()) {

                saveErrors(request, errors);
            }
        }

        // Set the target to failure
        if (employes == null) {
            target = new String("login");
        } else {
            request.setAttribute("employes", employes);
        }
        // Forward to the appropriate View
        return (mapping.findForward(target));
    }

    public ActionForward userEditAction(ActionMapping mapping,
            ActionForm form,
            HttpServletRequest request,
            HttpServletResponse response) throws ServiceException, UserLoginException {
        // Cible par defaut en cas de succes
        String target = new String("success");


        // Teste sur l'identification de l'utilisateur
        HttpSession session = request.getSession();
        if (session.getAttribute("USER") == null) {

            // L'utilisateur n'est pas identifie
            target = new String("login");
            ActionMessages errors = new ActionMessages();

            errors.add(ActionMessages.GLOBAL_MESSAGE,
                    new ActionMessage("errors.notConnected"));

            // Retourner les erreurs eventuelles au formulaire
            // d'origine.
            if (!errors.isEmpty()) {

                saveErrors(request, errors);
            }
            // Transmettre la requete a la vue appropriee
            return (mapping.findForward(target));
        }

        if (isCancelled(request)) {
            // Action annulee
            return (mapping.findForward(target));
        }

        try {
            EmployeForm eForm = (EmployeForm) form;
            Employe employe = getEmployeFromEmployeForm(eForm);
            EyrollesModele modele = getModel();
            modele.modifierEmploye(employe, eForm.getPassword());
        } catch (ServiceException dbe) {

            target = new String("error");
            ActionMessages errors = new ActionMessages();

            errors.add(ActionMessages.GLOBAL_MESSAGE,
                    new ActionMessage("errors.service.error",
                    dbe.getMessage()));

            // Signaler les erreurs eventuelles
            if (!errors.isEmpty()) {

                saveErrors(request, errors);
            }
        } catch (DataAccessException dae) {
            target = new String("error");
            ActionMessages errors = new ActionMessages();

            errors.add(ActionMessages.GLOBAL_MESSAGE,
                    new ActionMessage("errors.dataAccess.error", dae));


            // Enregistrer les erreurs  trouvees dans le formulaire original
            if (!errors.isEmpty()) {

                saveErrors(request, errors);
            }
        }

        // Transmettre la requete a la vue appropriee
        return (mapping.findForward(target));
    }

    public ActionForward userDeleteAction(ActionMapping mapping,
            ActionForm form,
            HttpServletRequest request,
            HttpServletResponse response) throws ServiceException, UserLoginException {
        // Cible par defaut en cas de succès
        String target = new String("success");

        // Teste si l'utilisateur est identifie
        HttpSession session = request.getSession();
        if (session.getAttribute("USER") == null) {

            // L'utilisateur n'est pas identifie
            target = new String("login");
            ActionMessages errors = new ActionMessages();

            errors.add(ActionMessages.GLOBAL_MESSAGE,
                    new ActionMessage("errors.notConnected"));

            // Signaler les erreurs eventuelles
            // au formulaire originel
            if (!errors.isEmpty()) {
                saveErrors(request, errors);
            }
        }

        try {
            EyrollesModele modele = getModel();
            modele.supprimerEmploye(request.getParameter("username"));
        } catch (ServiceException e) {
            target = new String("error");
            ActionMessages errors = new ActionMessages();

            errors.add(ActionMessages.GLOBAL_MESSAGE,
                    new ActionMessage("errors.service.error",
                    e.getMessage()));

            // Signaler les erreurs eventuelles
            if (!errors.isEmpty()) {

                saveErrors(request, errors);
            }
        } catch (DataAccessException dae) {
            target = new String("error");
            ActionMessages errors = new ActionMessages();

            errors.add(ActionMessages.GLOBAL_MESSAGE,
                    new ActionMessage("errors.dataAccess.error", dae));


            // Enregistrer les erreurs  trouvees dans le formulaire original
            if (!errors.isEmpty()) {

                saveErrors(request, errors);
            }
        }

        // Transmission a la vue appropriee
        return (mapping.findForward(target));
    }

    public ActionForward userAddAction(ActionMapping mapping,
            ActionForm form,
            HttpServletRequest request,
            HttpServletResponse response) throws ServiceException, UserLoginException {
        // Cible par defaut en cas de succes
        String target = new String("success");

        // teste l'identification de l'utilisateur ?
        HttpSession session = request.getSession();
        if (session.getAttribute("USER") == null) {

            // L'utilisateur n'est pas identifie
            target = new String("login");
            ActionMessages errors = new ActionMessages();

            errors.add(ActionMessages.GLOBAL_MESSAGE,
                    new ActionMessage("errors.notConnected"));

            // Renvoyer les erreurs au formulaire originel
            if (!errors.isEmpty()) {
                saveErrors(request, errors);
            }
            // Transmission a la vue appropriee
            return (mapping.findForward(target));
        }

        if (isCancelled(request)) {
            // Annulation. Retour a la liste des employes
            return (mapping.findForward(target));
        }

        try {
            EmployeForm eForm = (EmployeForm) form;
            Employe employe = getEmployeFromEmployeForm(eForm);
            EyrollesModele modele = getModel();
            modele.ajouterEmploye(employe, eForm.getPassword());
        } catch (ServiceException e) {
            target = new String("error");
            ActionMessages errors = new ActionMessages();

            errors.add(ActionMessages.GLOBAL_MESSAGE,
                    new ActionMessage("errors.service.error", e.getMessage()));

            // Signalement des erreurs eventuelles
            if (!errors.isEmpty()) {
                saveErrors(request, errors);
            }
        } catch (DataAccessException dae) {
            target = new String("error");
            ActionMessages errors = new ActionMessages();

            errors.add(ActionMessages.GLOBAL_MESSAGE,
                    new ActionMessage("errors.dataAccess.error", dae));


            // Enregistrer les erreurs  trouvees dans le formulaire original
            if (!errors.isEmpty()) {

                saveErrors(request, errors);
            }
        }

        // Transmission a la vue appropriee
        return (mapping.findForward(target));
    }

    public ActionForward getEmploye(ActionMapping mapping,
            ActionForm form,
            HttpServletRequest request,
            HttpServletResponse response) throws ServiceException, UserLoginException {
        // Cible par defaut en cas de succes
        String target = new String("success");

        // Teste sur l'identification de l'utilisateur
        HttpSession session = request.getSession();
        if (session.getAttribute("USER") == null) {

            // L'utilisateur n'est pas identifie
            target = new String("login");
            ActionMessages errors = new ActionMessages();

            errors.add(ActionMessages.GLOBAL_MESSAGE,
                    new ActionMessage("errors.notConnected"));

            // Retourner les erreurs eventuelles au formulaire d'origine
            if (!errors.isEmpty()) {

                saveErrors(request, errors);
            }

        }

        if (isCancelled(request)) {
            // Annulation depuis la liste des employes
            return (mapping.findForward(target));
        }

        try {

            EmployeForm eForm = null;

            Employe employe = null;
            String password = null;

            EyrollesModele modele = getModel();
            String username = request.getParameter("username");
            employe = modele.recupererEmploye(username);
            password = modele.recupererPassword(username);

            eForm = new EmployeForm();
            eForm.setUsername(employe.getUsername());
            eForm.setDepid(Integer.toString(employe.getDepid()));
            eForm.setRoleid(Integer.toString(employe.getRoleid()));
            eForm.setName(employe.getName());
            eForm.setPhone(employe.getPhone());
            eForm.setEmail(employe.getEmail());
            eForm.setPassword(password);
            form = eForm;
            if ("request".equals(mapping.getScope())) {
                request.setAttribute(mapping.getAttribute(), form);
            } else {
                session = request.getSession();
                session.setAttribute(mapping.getAttribute(), form);
            }
        } catch (UserNotFoundException unfe) {

            target = new String("error");
            ActionMessages errors = new ActionMessages();

            errors.add(ActionMessages.GLOBAL_MESSAGE,
                    new ActionMessage("errors.login.unknown", unfe.getLogin()));

            // Retourner les erreurs eventuelles
            if (!errors.isEmpty()) {

                saveErrors(request, errors);
            }
        } catch (ServiceException dbe) {

            target = new String("error");
            ActionMessages errors = new ActionMessages();

            errors.add(ActionMessages.GLOBAL_MESSAGE,
                    new ActionMessage("errors.service.error", dbe.getMessage()));

            // Retourner les erreurs eventuelles
            if (!errors.isEmpty()) {

                saveErrors(request, errors);
            }
        } catch (DataAccessException dae) {
            target = new String("error");
            ActionMessages errors = new ActionMessages();

            errors.add(ActionMessages.GLOBAL_MESSAGE,
                    new ActionMessage("errors.dataAccess.error", dae));


            // Enregistrer les erreurs  trouvees dans le formulaire original
            if (!errors.isEmpty()) {

                saveErrors(request, errors);
            }
        }

        // Transmettre la requete a la vue appropriee
        return (mapping.findForward(target));
    }
}
